package com.example.mvptest.model;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.example.mvptest.presenter.IPresenter;

public class HandleModel implements  IModel {
        private IPresenter presenter = null;
        private Handler handler = new Handler(Looper.getMainLooper());

        public void handleData(String data) {
                if (TextUtils.isEmpty(data)) {
                        return;
                }
                handler.removeCallbacksAndMessages(null);
                // 延迟来模拟网络或者磁盘操作
                handler.postDelayed(new Runnable() {
                        // 数据处理完成，通知 Presenter
                        @Override
                        public void run() {
                                presenter.dataHandled("handled data: $data");
                        }
                }, 3000);

        }
        public void clearData () {
                handler.removeCallbacksAndMessages(null);
                // 数据清理完成，通知 Presenter
                presenter.dataCleared();
        }

        public void setPresenter (IPresenter presenter){
                this.presenter = presenter;
        }
}